﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;

 /* Author : Shantha Kumar .T
  * Project Started : 19th December 2011 */

namespace SPTools.EmptyItems
{
    public partial class Form1 : Form
    {


        SPSite site = null;
        SPWeb web = null;
        string strweb = null;


        public Form1()
        {
            InitializeComponent();
        }

        private void btnGetLists_Click(object sender, EventArgs e)
        {
            //Cursor.Current = Cursors.WaitCursor;
            site = new SPSite(txtSite.Text);
            
            var vweb = from SPWeb w in site.AllWebs
                       select w;

            List<SPWeb> lweb = vweb.ToList<SPWeb>();
            cbWebs.DisplayMember = "Title";
            cbWebs.ValueMember = "ServerRelativeUrl";            
            cbWebs.DataSource = lweb;
            

            
                   

        }

        private void btnEmpty_Click(object sender, EventArgs e)
        {

            
            for (int i = 0; i < lstbx.SelectedIndices.Count; i++)
            {
                int tst = lstbx.SelectedIndices[i];
                ClearListItems(lstbx.Items[tst].ToString(), strweb);                
            }
        }

        public  void ClearListItems(string listname, string weburl)
        {

            using (SPSite _site = new SPSite(txtSite.Text))
            {
                using (SPWeb web = _site.OpenWeb(weburl))
                {
                    SPList list = web.Lists.TryGetList(listname);
                    string deletebatch = buildbatchupdate(list);

                    web.ProcessBatchData(deletebatch);

                    txtInfo.Text += "List : " + list.Title + "\r\n Total Items : " + list.ItemCount.ToString() + "\r\n\n";
                }
            }

            MessageBox.Show("All items are removed from "+ listname );


        }

        private static string buildbatchupdate(SPList listname)
        {

            string strmethod = "<Method><SetList Scope=\"Request\">" + listname.ID.ToString() + "</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>";

            StringBuilder strbatch = new StringBuilder();
            strbatch.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            strbatch.Append("<ows:Batch OnError=\"Return\">");


            foreach (SPListItem item in listname.Items)
                strbatch.Append(string.Format(strmethod, item.ID.ToString()));

            strbatch.Append("</ows:Batch>");

            return strbatch.ToString();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void lstbx_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtInfo.Text = null;
            
            foreach (SPList selecteditem in (sender as ListBox).SelectedItems)
            {

                txtInfo.Text += "List : " + selecteditem.Title + "\r\n Total Items : " + selecteditem.ItemCount.ToString() + "\r\n\n";
            }
        }

        private void cbWebs_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstbx.DataSource = null;
            strweb = null;
            lstbx.Items.Clear();
           
            strweb = cbWebs.SelectedValue.ToString();
            //MessageBox.Show(strurl);
            web = site.OpenWeb(strweb);
            SPListCollection lists = web.Lists;

            var aa = from SPList l in web.Lists
                     where !l.Hidden
                     select l;

            List<SPList> aaa = aa.ToList<SPList>();
            lstbx.DisplayMember = "Title";
            lstbx.ValueMember = "ID";
            
            lstbx.DataSource = aaa;

            //Cursor.Current = Cursors.Default;
            web.Dispose();
            site.Dispose();
            //MessageBox.Show("Completed Successfully");
        }
    }
}
